package cn.edu.scau.cmi.chenruilin.hibernate;

import java.util.List;
import org.hibernate.LockOptions;
import org.hibernate.Query;
import org.hibernate.Session;
import org.hibernate.Transaction;
import org.hibernate.criterion.Example;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;



public class StudentDao {
	private static final Logger log = LoggerFactory.getLogger(StudentDao.class);
	public static final String NAME = "name";
	private Session session;
	
	
	public Session getSession() {
		session = ScauCmiHibernateSessionFactoryUtil.getSession();
		return session;
	}
	
	public void save(Student instance) {
		log.debug("saving Student instance");
		try {
			
			//getSession().save(instance);
			getSession();
			Transaction transaction = session.beginTransaction();
			session.save(instance);
			transaction.commit();
			log.debug("save successful");
		} 
		catch (RuntimeException re) {
			log.error("save failed", re);
			throw re;
		}
	}
	
	public void delete(Student instance) {
		log.debug("deleting Student instance");
		try {
			getSession();
			Transaction transaction = session.beginTransaction();
			session.delete(instance);
			transaction.commit();
			log.debug("delete successful");
		}
		catch (RuntimeException re) {
			log.error("delete failed", re);
			throw re;
		}
	}
	
	public void update(Student instance) {
		log.debug("updating Student");
		try {
			getSession();
			Transaction transaction = session.beginTransaction();
			session.update(instance);
			transaction.commit();
			log.debug("update successful");
		} catch (RuntimeException re) {
			log.error("update failed", re);
			throw re;
		}
	}
	
	
	public List findAll() {
		log.debug("finding all student");
		try {
			String queryStr = "from Student";
			Query queryObject = getSession().createQuery(queryStr);
			log.debug("find by property successful");
			return queryObject.list();
		}
		catch (RuntimeException re) {
			log.error("find all failed", re);
			throw re;
		}
		
	}
	

	
	//下面的都没用了好像
	
	/*
	public Student findById(Long id) {
		log.debug("finding Student instance with id: " + id);
		try {
			Student result = (Student) getSession().get("cn.edu.scau.cmi.chenruilin.orm.Student", id);
			log.debug("find by id successful");
			return result;
		}
		catch (RuntimeException re) {
			log.error("find by id failed", re);
			throw re;
		}
	}
	
	public List findByExample(Student instance) {
		log.debug("finding Student instance with example");
		try {
			List results = getSession().createCriteria("cn.edu.scau.cmi.chenruilin.orm.Student")
					.add(Example.create(instance)).list();
			log.debug("find by example successful");
			return results;
		}
		catch (RuntimeException re) {
			log.error("find by example failed", re);
			throw re;
		}
	}
	
	public List findByProperty(String propertyName, Object value) {
		log.debug("finding Student instance with property: " + propertyName + ", value: " + value);
		try {
			String queryStr = "from Student as model where model." + propertyName + "= ?";
			Query queryObject = getSession().createQuery(queryStr);
			queryObject.setParameter(0, value);
			log.debug("find by property successful");
			return queryObject.list();
		}
		catch (RuntimeException re) {
			log.error("find by property name failed", re);
			throw re;
		}
		
	}
	
	public void insertTutor(Long id, Long t_id) {
		try {
			getSession();
			Transaction transaction = session.beginTransaction(); 
			String updateStr = "update Student s set s.teacher = " + t_id + "where id = " + id;
		    //String url = "update Teacher t set t.birthPlace = 'anhui' where id = 1"; 
		    //注意这里的Teacher必须是对象名字，而不是表名，t是对象的别名 
		    Query q = session.createQuery(updateStr); 
		    q.executeUpdate(); 
		    transaction.commit(); 
		}
		catch (RuntimeException re) {
			throw re;
		}
	}
	
	
	
	public List findByName(String name) {
		return findByProperty("name", name);
	}
	

	public Student merge(Student instance) {
		log.debug("merging student");
		try {
			Student result = (Student) getSession().merge(instance);
			log.debug("merge successful");
			return result;
		}
		catch (RuntimeException re) {
			log.error("merge failed", re);
			throw re;
		}
		
	}
	
	public void attachDirty(Student instance) {
		log.debug("attaching dirty Student");
		try {
			getSession();
			Transaction transaction = session.beginTransaction();
			session.saveOrUpdate(instance);
			transaction.commit();
			log.debug("attach successful");
		} catch (RuntimeException re) {
			log.error("attach failed", re);
			throw re;
		}
	}

	public void attachClean(Student instance) {
		log.debug("attaching clean Student");
		try {
			getSession().buildLockRequest(LockOptions.NONE).lock(instance);
			log.debug("attach successful");
		} catch (RuntimeException re) {
			log.error("attach failed", re);
			throw re;
		}
	}
	
	*/
}
